<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class rp extends MY_Controller {

	function __construct(){
        // Call the Model constructor
        parent::__construct();
        $this->lang->load('vt');
        //$this->load->library('form_validation');
        //$this->load->helper('form');
        //$this->load->helper('date');
    }
    
	public function index()
	{
        $this->load->model('vt_model');
        
        $this->template->set_page_title( lang('title_tab_report') );
        $this->template->set_view('rp/content_home');
        
        $this->template->render();
	}
    
    public function datatable_filter_bill_import(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_filter_bill_import($_GET,1);
        $totalData = $this->vt_model->load_datatable_filter_bill_import($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
    
    public function datatable_filter_bill_export(){
        $this->load->model('vt_model');
        $aaData = $this->vt_model->load_datatable_filter_bill_export($_GET,1);
        $totalData = $this->vt_model->load_datatable_filter_bill_export($_GET,2);
        
        $output = array(
            "sEcho" => intval($_GET['sEcho']),
            "iTotalRecords" => $aaData['total'],
            "iTotalDisplayRecords" => $totalData['total'],//$iFilteredTotal,
            "aaData" => $aaData['aaData']
        );
    
        echo json_encode( $output );
    }
   
    public function report(){
        $this->load->model('vt_model');
        if ($_SERVER['REQUEST_METHOD'] == 'POST'){
            $report_date_from = $this->input->post('report_date_from');
            $report_date_to = $this->input->post('report_date_to');
            
            $date_from = $report_date_from;
            $date_to = $report_date_to;
            $reports = $this->vt_model->getFilterReport($date_from,$date_to);
            
            $this->template->set('report_date_from',$report_date_from);
            $this->template->set('report_date_to',$report_date_to);
            
            if($reports){                
                $report_date = array();
                $report_data = array();
                $report_import = array();
                $report_export = array();
                $report_revert = array();
                $revert_array = array();
                foreach($reports As $report){
                    $report_date[$report->code] = $report->code;
                    $report_data[$report->code] = json_decode($report->data,true);
                    
                    $report_import[$report->code] = ( $report->bill_import != null ) ? json_decode($report->bill_import,true):null;
                    $report_export[$report->code] = ( $report->bill_export != null ) ? json_decode($report->bill_export,true):null;
                    $revert_array[$report->code] = ( $report->revert != null ) ? json_decode($report->revert,true):null;
                    
                }
                
                $products = $this->vt_model->getALLProductData();
                
                $data_array_total = array();
                
                foreach($products As $product){
                    $flag = false;
                    $arr_total = array(
                        'id' => $product->id,
                        'code' => $product->code,
                        'name' => $product->name,
                        'value' => $product->val_name,
                        'category' => $product->catg_name,
                        'first_total' => 0,
                        'import' => 0,
                        'export' => 0,
                        'revert' => 0,
                        'last_total' => 0,
                        'price' => 0
                    );

                    foreach($report_data As $key => $re){
                        if( isset($re[$product->id]) && ($re[$product->id]['it'] != 0 || $re[$product->id]['et'] != 0 || $re[$product->id]['rt'] != 0) ){
                            $flag = true;
                            $arr_total['import'] += $re[$product->id]['it'];
                            $arr_total['export'] += $re[$product->id]['et'];
                            $arr_total['revert'] += $re[$product->id]['rt'];
                            $arr_total['first_total'] = $re[$product->id]['ft'];//lay gia tri ngay cuoi cung (chi so ban dau)
                        }
                    }
                    if($flag == true){
                        $arr_total['last_total'] = $arr_total['first_total'] + $arr_total['import'] - $arr_total['export'] + $arr_total['revert'];
                        $data_array_total[] = $arr_total;
                    }
                    
                    $re_count = 0;
                    $flag_re = false;
                    foreach($revert_array As $revs){
                        if( isset($revs[$product->id]) ){
                            $flag_re = true;
                            $re_count += $revs[$product->id][2];
                        }
                    }
                    if($flag_re == true){
                        $report_revert[] = array(
                            'id' => $product->id,
                            'code' => $product->code,
                            'name' => $product->name,
                            'value' => $product->val_name,
                            'category' => $product->catg_name,
                            'total' => $re_count
                        );
                    }
                }
                
                if( count($report_import) > 0 ){
                    $data_import = array();
                    foreach($report_import As $key_imp => $rimp){
                        $arr_tmp = array(
                            'date' => $key_imp,
                            'total_novat' => 0,
                            'total_vat' => 0
                        );
                        if($rimp != null){
                            foreach($rimp As $imp){
                                $arr_tmp['total_novat'] += $imp[4];
                                $arr_tmp['total_vat'] += $imp[7];
                            }
                        }
                        
                        $data_import[] = $arr_tmp;
                    }
                    $this->template->set('data_import',$data_import);
                }
                
                if( count($report_export) > 0 ){
                    $data_export = array();
                    foreach($report_export As $key_exp => $rexp){
                        if($rexp != null){
                            $ar_tmp = array(
                                'date' => $key_exp,
                                'total_novat' => 0,
                                'total_vat' => 0
                            );
                            foreach($rexp As $exp){
                                $ar_tmp['total_novat'] += $exp[4];
                                $ar_tmp['total_vat'] += $exp[7];
                            }
                            $data_export[] = $ar_tmp;
                        }
                    }
                    $this->template->set('data_export',$data_export);
                }
                
                if( count($report_revert) > 0 ){
                    $this->template->set('report_revert',$report_revert);
                }
                //var_dump( count($data_array) );
                $this->template->set('data_total',$data_array_total);
                $this->template->set('report_date',$report_date);
                //$this->template->set_view('rp/content_report');
                
                //$this->template->render();
            }
            
        }else{
            $report_date_from = date('Y-m-d');
            $report_date_to = '';
            $this->template->set('report_date_from',$report_date_from);
            $this->template->set('report_date_to',$report_date_to);
        }
        
        $this->template->set_page_title( lang('menu_baocaothongke') );
        $this->template->set_view('rp/content_report');
        $this->template->render();
    }
    
    public function ajax_reportdate($date){
        $this->load->model('vt_model');
        
        $products = $this->vt_model->getALLProductData();
        
        $report = $this->vt_model->getReportByCode($date);
        $report_data = json_decode($report->data,true);
        
        $import_data = ( $report->bill_import != null ) ? json_decode($report->bill_import,true):null;
        $export_data = ( $report->bill_export != null ) ? json_decode($report->bill_export,true):null;
        $revert_array = ( $report->revert != null ) ? json_decode($report->revert,true):null;
        $revert_data = array();
        $data_array = array();
        foreach($products As $product){
            if( isset($report_data[$product->id]) ){
                $data_array[] = array(
                    'id' => $product->id,
                    'code' => $product->code,
                    'name' => $product->name,
                    'value' => $product->val_name,
                    'category' => $product->catg_name,
                    'first_total' => $report_data[$product->id]['ft'],
                    'import' => $report_data[$product->id]['it'],
                    'export' => $report_data[$product->id]['et'],
                    'revert' => $report_data[$product->id]['rt'],
                    'price' => $report_data[$product->id]['p']
                );
            }
            if( isset($revert_array[$product->id]) ){
                $revert_data[] = array(
                    'id' => $product->id,
                    'code' => $product->code,
                    'name' => $product->name,
                    'count' => $revert_array[$product->id][2],
                    'price' => $revert_array[$product->id][3]
                );
            }
        }
        
        $this->load->view($this->template->template.'/rp/table_report',array(
            'id_tab'=>$date,
            'data'=>$data_array,
            'import_data'=>$import_data,
            'export_data'=>$export_data,
            'revert_data'=>$revert_data
        ));
    }
    
    public function vt(){
        $this->load->model('vt_model');
        
        $products = $this->vt_model->getProductBystatus(1);
        
        $category_data = array();
        $count_total = 0;
        foreach($products As $pro){
            if( !isset($category_data[$pro->catg_id]) ){
                $category_data[$pro->catg_id] = array(
                    'catg_id' => $pro->catg_id,
                    'catg_name' => $pro->catg_name
                );
            }
            $count_total += $pro->total*$pro->price;
        }
        $this->template->set('category_data',$category_data);
        $this->template->set('count_total',$count_total);
        $this->template->set_page_title( lang('title_tab_report_vt') );
        $this->template->set_view('rp/content_vt');
        
        $this->template->render();
    }
    
    public function ajax_vt($catg_id){
        $this->load->model('vt_model');
        
        $products = $this->vt_model->getProductByCategory($catg_id);
        
        $this->load->view($this->template->template.'/rp/list_product',array('products'=>$products));
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */